package com.bqtt.tumor.demo.common;

import java.net.URL;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.bqtt.tumor.demo.common.utility.InlineTools;


public class MyBeanFactory implements ApplicationContextAware{

	public static ApplicationContext applicationContext = null;
	
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
	{
		
		//初始化本地根目录变量
		URL rootDirectory = MyBeanFactory.class.getClassLoader().getResource("logback.xml");
		
		InlineTools.imgLocalPathDirectory = rootDirectory.getPath().replace("WEB-INF/classes/logback.xml", "resources/medical_images/");
		
		if(this.applicationContext==null)
		{
			this.applicationContext =applicationContext;
		}
		
		System.out.println("ApplicationContext setter is called...");
		
	}

	public static ApplicationContext getApplicationContext() {
		
			if( applicationContext==null)
			{ //本地调试
				 applicationContext = new FileSystemXmlApplicationContext("/src/main/resources/");
			}
	     	return applicationContext;
	}
	
	public static Object getBean(String name)
	{
		if(applicationContext==null)
		{
			applicationContext = getApplicationContext();
		}
		return applicationContext.getBean(name);
	}
}
